// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Lava Flow Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 60 : TYPE_LAVAFLOW

// Function declarations
reserve function LavaFlow_DebugDraw
reserve function LavaFlow_DebugSpawn

// Static Values

// Tables

function LavaFlow_DebugDraw
	DrawSprite(0)
end function


function LavaFlow_DebugSpawn
	CreateTempObject(TypeName[Lava Flow], 0, object.xpos, object.ypos)
end function


event ObjectMain
	switch object.state
	case 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -128, -33, 160, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				CallFunction(PlayerObject_FireHit)
			end if
			BoxCollisionTest(C_BOX, object.entityPos, -128, -31, 128, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			BoxCollisionTest(C_TOUCH, object.entityPos, 256, -32, 288, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object.state++
				object.priority = PRIORITY_ACTIVE
			end if
		next
		break
	case 1
		if object[0].state != PlayerObject_Death
			if object[0].state != PlayerObject_Knockback
				object.xpos += 0x18000
				if object.xpos >= 0x6480000
					object.xpos = 0x6480000
					object.state++
				end if
			end if
		end if
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -128, -32, 160, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				CallFunction(PlayerObject_FireHit)
			end if
			BoxCollisionTest(C_BOX, object.entityPos, -128, -31, 128, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			BoxCollisionTest(C_TOUCH, object.entityPos, -128, -23, 128, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object[currentPlayer].ixpos = object[currentPlayer].collisionLeft
				FlipSign(object[currentPlayer].xpos)
				if object.xpos < 0x5800000
					object[currentPlayer].xpos += 0x800000
				else
					object[currentPlayer].xpos += 0x600000
				end if
				object[currentPlayer].xpos += object.xpos
			end if
		next
		break
	case 2
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -128, -32, 160, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				CallFunction(PlayerObject_FireHit)
			end if
			BoxCollisionTest(C_BOX, object.entityPos, -128, -24, 160, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			BoxCollisionTest(C_TOUCH, object.entityPos, -128, -23, 128, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object[currentPlayer].ixpos = object[currentPlayer].collisionLeft
				FlipSign(object[currentPlayer].xpos)
				object[currentPlayer].xpos += 0x600000
				object[currentPlayer].xpos += object.xpos
			end if
		next
		if object.outOfBounds == 1
			temp0 = object.xpos
			object.xpos = object.value1
			if object.outOfBounds == 1
				object.state = 0
			else
				object.xpos = temp0
			end if
		end if
		break
	end switch
	Sin(object.value2, object[10].value3)
	object.value2 += 512
	object.value2 >>= 2
	object.value2 &= 31
	object.value2++
	object.frame = object.animationTimer
	object.frame /= 10
	object.frame += 4
	object.animationTimer++
	object.animationTimer %= 40
	switch object[10].frame
	case 0
		EditFrame(0, -160, -32, 160, 32, object.value2, 405)
		EditFrame(1, 0, -32, 124, 32, object.value2, 405)
		EditFrame(2, -160, 0, 160, 32, object.value2, 405)
		EditFrame(3, 0, 0, 156, 32, object.value2, 405)
		break
	case 1
		EditFrame(0, -160, -32, 160, 32, object.value2, 438)
		EditFrame(1, 0, -32, 124, 32, object.value2, 438)
		EditFrame(2, -160, 0, 160, 32, object.value2, 438)
		EditFrame(3, 0, 0, 156, 32, object.value2, 438)
		break
	case 2
		EditFrame(0, -160, -32, 160, 32, object.value2, 471)
		EditFrame(1, 0, -32, 124, 32, object.value2, 471)
		EditFrame(2, -160, 0, 160, 32, object.value2, 471)
		EditFrame(3, 0, 0, 156, 32, object.value2, 471)
		break
	end switch
end event


event ObjectDraw
	DrawSprite(0)
	DrawSprite(1)
	DrawSprite(2)
	DrawSprite(3)
	DrawSprite(object.frame)
end event


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-128, -32, 128, 32, 1, 405)
	SpriteFrame(0, -32, 128, 32, 1, 405)
	SpriteFrame(-128, 0, 256, 32, 1, 405)
	SpriteFrame(0, 0, 256, 32, 1, 405)
	SpriteFrame(124, -32, 60, 64, 1, 194)
	SpriteFrame(124, -32, 60, 64, 62, 194)
	SpriteFrame(124, -32, 60, 64, 1, 259)
	SpriteFrame(124, -32, 60, 64, 62, 259)
	foreach (TypeName[Lava Flow], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value1 = object[arrayPos0].xpos
		object[arrayPos0].drawOrder = 4
	next
	SetTableValue(TypeName[Lava Flow], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(LavaFlow_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(LavaFlow_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
